s1_read_last_two_bytes_of_a_mbr_backup_file(){
	if ! command -v xxd &> /dev/null; then
		echo "NO"
		return
	fi
	if [ ! -f "$1" ]; then
		echo "NO"
		return
	fi
	local last_bytes=$(xxd -p -l 2 -s 510 "$1" | tr -d '\n')
	[[ "${last_bytes^^}" == "55AA" ]] && echo "YES" || echo "NO"
}

s2_read_mbr_partition_table_info(){
	if ! command -v xxd &> /dev/null; then
		echo "-1 -1 -1 -1"
		return
	fi

	local result=() 
	for i in {0..3}; do
		local offset=$((446 + i *16))
		local type=$(xxd -p -l 1 -s  $((offset + 4 )) "$1" | tr -d '\n')

		if [ "$type" != "00" ]; then
			local start_hex=$(xxd -p -l 4 -s $((offset + 8)) "$1" | tr -d '\n')
			local sectors_hex=$(xxd -p -l 4 -s $((offset + 12)) "$1" | tr -d '\n')
			start_hex=$(echo $start_hex | sed -E 's/(..)(..)(..)(..)/\4\3\2\1/')
			sectors_hex=$(echo $sectors_hex | sed -E 's/(..)(..)(..)(..)/\4\3\2\1/')
			local start_dec=$((16#$start_hex))
			local sectors_dec=$((16#$sectors_hex))
			local end_dec=$((start_dec + sectors_dec - 1))
			result+=("$start_dec" "$end_dec")
		fi
	done 
	
	if [ ${#result[@]} -eq 0 ]; then
		echo "-1 -1 -1 -1"
	else
		echo "${result[0]} ${result[1]} ${result[0]} ${result[1]}"
	fi
}

s3_parse_partition_info(){
	local output=$(cat)

	local disk_bytes=$(echo "$output" | grep -E "Disk.*bytes" | grep -oE "[0-9]+ bytes" | head -1 | grep -oE "[0-9]+")
	local disk_sectors=$(echo "$output" | grep -E "Disk.*sectors" | grep -oE "[0-9]+" | tail -1)
	local sector_size=$(echo "$output" | grep -i "Sector size" | grep -oE "[0-9]+" | head -1)

	local first_part=$(echo "$output" | awk '/^\/dev/ {print; exit}')
	local start=$(echo "$first_part" | awk '{print $3}')
	local end=$(echo "$first_part" | awk '{print $4}')
	local type=$(echo "$first_part" | awk '{print $NF}')

	echo "disk size in bytes:$disk_bytes"
	echo "disk size in sectors:$disk_sectors"
	echo "physical sector size in bytes:$sector_size"
	echo "first partition start sector id:$start"
	echo "first partition end sector id:$end"
	echo "first partition type:$type"
}

s4_run_passwd_as_plain_user_and_get_status(){
	local path=$(which oversleeping)
	sudo chown root "$path"
	sudo chmod 4755 "$path"
	"$path" &
	sleep 1

	local info=$(ps -o uid,euid,ruid -C oversleeping | tail -n 1)
	local uid=$(echo "$info" | awk '{print $1}')
	local euid=$(echo "$info" | awk '{print $2}')
	local ruid=$(echo "$info" | awk  '{print $3}')

	echo "$uid,$euid,$ruid"
}

s5_add_a_scheduled_task(){
	echo "*/5 * * * * echo \$(date +\\%Y\\%m\\%d_\\%H\\%M\\%S) >> \$HOME/test_crontab" | crontab -
}
